package com.zucc.eye.util;

import com.alibaba.fastjson.JSONObject;
import com.zucc.eye.entity.GitPageRepo;
import com.zucc.eye.repository.GitPageRepoRepository;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class GitPageUtil {

    public static void main(String[] args) throws IOException {
        List<GitPageRepo> githubPageRepos = getGithubPageRepos("java",2);
        System.out.println("githubPageRepos");
        System.out.println(githubPageRepos);
    }
//    GitPageRepoRepository co
   public  static        List<GitPageRepo> getGithubPageRepos(   String query,Integer page) throws IOException {
//        String queryMark="q";
//        String queryMark="#queryMark#";
//        String getUrl="https://github.com/search?q=java";
//        String getUrl="https://github.com/search?q=#queryMark#";
//        String getUrl="https://github.com/search?p=2&q="+query+"&type=Repositories";
//        String query="java";
       String getUrlMarked ="https://github.com/search?p="+page+"&q="+query+"&type=Repositories";
//        String getUrlMarked = getUrl.replace(queryMark, query);
//       https://github.com/search?p=2&q=java&type=Repositories
        Document newDoc = Jsoup.connect(getUrlMarked).get();
//       Jsoup.connect(Ur)
//       url query 拼接 java
//        Element element = newDoc.select("*[@id=\"js-pjax-container\"]/div/div[3]/div/ul/li[1]/div[2]/div[1]/div[1]/a").get(0);

//       https://acc15t4bm5.feishu.cn/docs/doccnKlHj7Nm4Ju9oD14nZ4F5Og
//       测试 数据
//        Element repoUl= newDoc.select("#js-pjax-container > div > div.col-12.col-md-9.float-left.px-2.pt-3.pt-md-0.codesearch-results > div > ul").get(0);
//        Elements repoLis= newDoc.select("#js-pjax-container > div > div.col-12.col-md-9.float-left.px-2.pt-3.pt-md-0.codesearch-results > div > ul > li ");
        Elements repoLis= newDoc.select("#js-pjax-container > div > div.col-12.col-md-9.float-left.px-2.pt-3.pt-md-0.codesearch-results > div > ul > li");
//       Jsoup  ul 下面的所有
//       Elements ElementsUl = newDoc.getElementsByTag("ul");
       List<GitPageRepo>list=new ArrayList<>();
       for (Element elementLi : repoLis) {
//           elementLi.select()
           Elements repo1Dom = elementLi.select("div.mt-n1.flex-auto");
           Elements sketch = repo1Dom.select(" p");
           String repoName = repo1Dom.select("div.d-flex > div.f4.text-normal > a").get(0).text();

           String sketchText = sketch.text();
           System.out.println("sketchText");
           System.out.println(sketchText);
           Elements topics = repo1Dom.select("  div:nth-child(3) > div:nth-child(1) > a");

//           Elements topics = repo1Dom.select("  div:nth-child(3) > div:nth-child(1) > a");

//           for (Element topic : topics) {
//               String textTopic = topic.text();
//           }
           List<String> topicTexts = topics.stream().map(o -> {
               return o.text();
           }).collect(Collectors.toList());
//           topicTexts.st

//           Elements topic = repo1Dom.select("  div:nth-child(3) > div:nth-child(1) > a:nth-child(1)");
//           Elements topic2 = repo1Dom.select(" div:nth-child(3) > div:nth-child(1) > a:nth-child(2)");
           Elements star = repo1Dom.select("  div:nth-child(3) > div.d-flex.flex-wrap.text-small.color-fg-muted > div:nth-child(1) > a");
           String textStar = star.text();
//           int starInt = Integer.parseInt(textStar);
//           String s = topicTexts.toString();
//           https://www.cnblogs.com/yelanggu/p/13261828.html
//           JsonUtil.
//           JSONObject.
//           JSONObject list转化为 string
           //实体类对象转换成String类型的JSON字符串
//           String s = JSONObject.toJSONString(topics);
           String topicsStr = JsonUtil.objToStr(topicTexts);


           GitPageRepo build = GitPageRepo.builder().repoName(repoName).
                   sketch(sketchText).star(textStar)
                   .topics(topicsStr).
                           queryWord(query).
                           page(page).build();
           list.add(build);
       }

       return list;
        //     仓库名字
//       Elements allElements = repoUl.getAllElements();
////       allElements.
//       Element repoName = newDoc.select("#js-pjax-container > div > div.col-12.col-md-9.float-left.px-2.pt-3.pt-md-0.codesearch-results > div > ul > li:nth-child(1) > div.mt-n1.flex-auto > div.d-flex > div.f4.text-normal > a").get(0);
//        Element repo1 = repoUl.select("li:nth-child(1)").get(0);
//        Elements repo1Dom = repo1.select("div.mt-n1.flex-auto");
//        Elements sketch = repo1Dom.select(" p");
//        Elements topic = repo1Dom.select("  div:nth-child(3) > div:nth-child(1) > a:nth-child(1)");
//        Elements topic2 = repo1Dom.select(" div:nth-child(3) > div:nth-child(1) > a:nth-child(2)");
//        Elements star = repo1Dom.select("  div:nth-child(3) > div.d-flex.flex-wrap.text-small.color-fg-muted > div:nth-child(1) > a");
////        System.out.println("sketch");
////        System.out.println(sketch);
////        System.out.println("topic");
////        System.out.println(topic);
////        System.out.println("topic2");
////        System.out.println(topic2);
////        System.out.println("star");
////        System.out.println(star);
////        System.out.println("repoName");
////        System.out.println(repoName);
//       String sketchText = sketch.text();
//       String topicText = topic.text();
//       System.out.println("sketchText");
//       System.out.println(sketchText);
//       System.out.println("topicText");
//       System.out.println(topicText);

//       sketchText
//       All Algorithms implemented in Java
//               topicText
//       algorithms
//        repo1.select("div.mt-n1.flex-auto");
//        Element repoName = repoUl.select(" li:nth-child(1) > div.mt-n1.flex-auto > div.d-flex > div.f4.text-normal > a").get(0);

//        Element sketch = newDoc.select("#js-pjax-container > div > div.col-12.col-md-9.float-left.px-2.pt-3.pt-md-0.codesearch-results > div > ul > li:nth-child(1) > div.mt-n1.flex-auto > p").get(0);
//        Element topic = newDoc.select("#js-pjax-container > div > div.col-12.col-md-9.float-left.px-2.pt-3.pt-md-0.codesearch-results > div > ul > li:nth-child(1) > div.mt-n1.flex-auto > div:nth-child(3) > div:nth-child(1) > a:nth-child(1)").get(0);
//        Element topic2 = newDoc.select("#js-pjax-container > div > div.col-12.col-md-9.float-left.px-2.pt-3.pt-md-0.codesearch-results > div > ul > li:nth-child(1) > div.mt-n1.flex-auto > div:nth-child(3) > div:nth-child(1) > a:nth-child(2)").get(0);
//        Element star = newDoc.select("#js-pjax-container > div > div.col-12.col-md-9.float-left.px-2.pt-3.pt-md-0.codesearch-results > div > ul > li:nth-child(1) > div.mt-n1.flex-auto > div:nth-child(3) > div.d-flex.flex-wrap.text-small.color-fg-muted > div:nth-child(1) > a").get(0);
//        Element element = newDoc.select("#js-pjax-container > div > div.col-12.col-md-9.float-left.px-2.pt-3.pt-md-0.codesearch-results > div > ul > li:nth-child(1) > div.mt-n1.flex-auto > div.d-flex > div.f4.text-normal > a").get(0);
//        *[@id="js-pjax-container"]/div/div[3]/div/ul/li[1]/div[2]/div[1]/div[1]/a
//        #js-pjax-container > div > div.col-12.col-md-9.float-left.px-2.pt-3.pt-md-0.codesearch-results > div > ul > li:nth-child(1) > div.mt-n1.flex-auto > div.d-flex > div.f4.text-normal > a
    }
}
